package com.maoxs.service;

import com.maoxs.dao.QuartzMapper;
import com.maoxs.pojo.QuartzEntity;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
@Deprecated //废弃
public class JobServiceImpl implements JobService {

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private QuartzMapper quartzMapper;

    /**
     * 新增任务
     *
     * @param quartz
     * @return
     * @throws Exception
     */
    @Override
    public String save(QuartzEntity quartz) throws Exception {
        log.info("新增任务");
        //获取Scheduler实例、废弃、使用自动注入的scheduler、否则spring的service将无法注入
        //Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        //如果是修改  展示旧的 任务
        if (quartz.getOldJobGroup() != null) {
            JobKey key = new JobKey(quartz.getOldJobName(), quartz.getOldJobGroup());
            scheduler.deleteJob(key);
        }
        Class cls = Class.forName(quartz.getJobClassName());
        cls.newInstance();
        //构建job信息
        JobDetail job = JobBuilder.newJob(cls)
                .withIdentity(quartz.getJobName(),
                quartz.getJobGroup())
                .withDescription(quartz.getDescription())
                .build();
        // 触发时间点
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartz.getCronExpression());
        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("trigger" + quartz.getJobName(), quartz.getJobGroup())
                .startNow()
                .withSchedule(cronScheduleBuilder)
                .build();
        //交由Scheduler安排触发
        scheduler.scheduleJob(job, trigger);
        return "OK";
    }

    /**
     * 任务列表
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<QuartzEntity> list() throws Exception {
        log.info("任务列表");
        List<QuartzEntity> list = quartzMapper.listQuartzEntity();
        return list;
    }

    /**
     * 触发任务
     *
     * @param quartz
     * @return
     * @throws Exception
     */
    @Override
    public String trigger(QuartzEntity quartz) throws Exception {
        log.info("触发任务");
        JobKey key = new JobKey(quartz.getJobName(), quartz.getJobGroup());
        scheduler.triggerJob(key);
        return "OK";
    }

    /**
     * 停止任务
     *
     * @param quartz
     * @return
     * @throws Exception
     */
    @Override
    public String pause(QuartzEntity quartz) throws Exception {
        log.info("停止任务");
        JobKey key = new JobKey(quartz.getJobName(), quartz.getJobGroup());
        scheduler.pauseJob(key);
        return "OK";
    }

    /**
     * 恢复任务
     *
     * @param quartz
     * @return
     * @throws Exception
     */
    @Override
    public String resume(QuartzEntity quartz) throws Exception {
        log.info("恢复任务");
        JobKey key = new JobKey(quartz.getJobName(), quartz.getJobGroup());
        scheduler.resumeJob(key);
        return "OK";
    }

    /**
     * 删除任务
     *
     * @param quartz
     * @return
     * @throws Exception
     */
    @Override
    public String remove(QuartzEntity quartz) throws Exception {
        log.info("删除任务");
        TriggerKey triggerKey = TriggerKey.triggerKey(quartz.getJobName(), quartz.getJobGroup());
        // 停止触发器
        scheduler.pauseTrigger(triggerKey);
        // 移除触发器
        scheduler.unscheduleJob(triggerKey);
        // 删除任务
        scheduler.deleteJob(JobKey.jobKey(quartz.getJobName(), quartz.getJobGroup()));
        log.info("删除{}组下{}任务", quartz.getJobGroup(), quartz.getJobName());
        return "OK";
    }
}
